package com.andrew.demo;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.AbsListView.OnScrollListener;

public class IphoneExpandableListActivity extends Activity implements
		OnScrollListener {

	private static final String TAG = "iphone";
	private static final String PRE = "IphoneExpandableListActivity--";

	private ExpandableListView listView;
	private MyExpandableListAdapter mAdapter;

	private LinearLayout indicatorGroup;
	private int indicatorGroupId = -1;
	private int indicatorGroupHeight;

	private LayoutInflater mInflater;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		listView = (ExpandableListView) findViewById(R.id.expandableListView);
		indicatorGroup = (LinearLayout) findViewById(R.id.topGroup);
		// indicatorGroup.setVisibility(View.INVISIBLE);

		mAdapter = new MyExpandableListAdapter();
		listView.setAdapter(mAdapter);
		listView.setOnScrollListener(this);
		listView.setGroupIndicator(null);
		// indicatorGroup.setOnClickListener(new OnClickListener() {
		//
		// public void onClick(View v) {
		// // TODO Auto-generated method stub
		// if (listView.isGroupExpanded(indicatorGroupId)){
		// listView.collapseGroup(indicatorGroupId);
		// indicatorGroupId = -1;
		// mAdapter.hideGroup(-1);
		// mAdapter.notifyDataSetChanged();
		// }
		// else{
		// listView.expandGroup(indicatorGroupId);
		// indicatorGroupId = -1;
		// mAdapter.hideGroup(-1);
		// mAdapter.notifyDataSetChanged();
		// }
		// }
		// });
		// copy group view to indicator Group
		mInflater.inflate(R.layout.list_item, indicatorGroup, true);
	}

	/**
	 * A simple adapter which maintains an ArrayList of photo resource Ids. Each
	 * photo is displayed as an image. This adapter supports clearing the list
	 * of photos and adding a new photo.
	 * 
	 */
	public class MyExpandableListAdapter extends BaseExpandableListAdapter {
		// Sample data set. children[i] contains the children (String[]) for
		// groups[i].
		private String[] groups = { "People Names", "Dog Names", "Cat Names",
				"Fish Names" };
		private String[][] children = {
				{ "Way", "Arnold", "Barry", "Chuck", "David", "Afghanistan",
						"Albania", "Belgium", "Lily", "Jim", "LiMing", "Jodan" },
				{ "Ace", "Bandit", "Cha-Cha", "Deuce", "Bahamas", "China",
						"Dominica", "Jim", "LiMing", "Jodan" },
				{ "Fluffy", "Snuggles", "Ecuador", "Ecuador", "Jim", "LiMing",
						"Jodan" },
				{ "Goldy", "Bubbles", "Iceland", "Iran", "Italy", "Jim",
						"LiMing", "Jodan" } };

		private int mHideGroupPos = -1;

		public Object getChild(int groupPosition, int childPosition) {
			return children[groupPosition][childPosition];
		}

		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		public int getChildrenCount(int groupPosition) {
			return children[groupPosition].length;
		}

		public TextView getGenericView() {
			// Layout parameters for the ExpandableListView
			AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
					ViewGroup.LayoutParams.MATCH_PARENT, 64);

			TextView textView = new TextView(IphoneExpandableListActivity.this);
			textView.setLayoutParams(lp);
			// Center the text vertically
			textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
			// Set the text starting position
			textView.setPadding(36, 0, 0, 0);
			textView.setTextColor(Color.BLACK);
			return textView;
		}

		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			TextView textView = getGenericView();
			textView.setText(getChild(groupPosition, childPosition).toString());
			return textView;
		}

		public Object getGroup(int groupPosition) {
			return groups[groupPosition];
		}

		public int getGroupCount() {
			return groups.length;
		}

		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		/**
		 * create group view and bind data to view
		 */
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			View v;
			if (convertView == null) {
				v = mInflater.inflate(R.layout.list_item, null);
			} else {
				v = convertView;
			}
			TextView textView = (TextView) v.findViewById(R.id.textView);

			textView.setText(getGroup(groupPosition).toString());
			if (mHideGroupPos == groupPosition) {
				textView.setVisibility(View.INVISIBLE);
			} else {
				textView.setVisibility(View.VISIBLE);
			}
			return v;
		}

		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}

		public boolean hasStableIds() {
			return true;
		}

		public void hideGroup(int groupPos) {
			mHideGroupPos = groupPos;
		}

	}

	private void updateGroupView(View v, boolean bShow) {
		TextView textView = (TextView) v.findViewById(R.id.textView);

		if (textView == null) {
			return;
		}
		if (bShow == false) {
			textView.setVisibility(View.INVISIBLE);
		} else {
			textView.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * here is very importance for indicator group
	 */
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {

		ExpandableListView listView = (ExpandableListView) view;
		// //当前第一行归属的组ID
		// int gid =
		// ExpandableListView.getPackedPositionGroup(listView.getExpandableListPosition(firstVisibleItem));
		// //当前第一行的子ID
		// int cid =
		// ExpandableListView.getPackedPositionChild(listView.getExpandableListPosition(firstVisibleItem));
		// //当前第二行的子ID
		// int nid =
		// ExpandableListView.getPackedPositionChild(listView.getExpandableListPosition(firstVisibleItem+1));
		//
		// MyExpandableListAdapter
		// adapter=(MyExpandableListAdapter)listView.getExpandableListAdapter();
		//
		// String gtitle=(gid>-1)?adapter.getGroup(gid).toString():"";
		//
		// //如果第一行和第二行都是组header，不需要显示
		// if(cid==-1 && nid==-1){
		// indicatorGroup.setVisibility(View.GONE);
		// }
		// else {
		// //当前真的是一个child，而且下一行是group
		// if(nid==-1 && cid>=0){
		// indicatorGroup.setVisibility(View.GONE);
		// return;
		// }
		// indicatorGroup.setVisibility(View.VISIBLE);
		// //
		// ((TextView)(indicatorGroup.findViewById(R.id.textView))).setText(gtitle);
		// // indicatorGroup.postInvalidate();
		// }
		/**
		 * calculate point (0,0)
		 */
		int npos = view.pointToPosition(0, 0);
		if (npos != AdapterView.INVALID_POSITION) {
			long pos = listView.getExpandableListPosition(npos);
			int childPos = ExpandableListView.getPackedPositionChild(pos);
			int groupPos = ExpandableListView.getPackedPositionGroup(pos);
			if (childPos == AdapterView.INVALID_POSITION) {
				View groupView = listView.getChildAt(npos
						- listView.getFirstVisiblePosition());
				indicatorGroupHeight = groupView.getHeight();
			}
			// get an error data, so return now
			if (indicatorGroupHeight == 0) {
				return;
			}
			// update the data of indicator group view
			if (groupPos != indicatorGroupId) {
				mAdapter.getGroupView(groupPos,
						listView.isGroupExpanded(groupPos),
						indicatorGroup.getChildAt(0), null);
				indicatorGroupId = groupPos;
				Log.e(TAG, PRE + "bind to new group,group position = "
						+ groupPos);
				mAdapter.hideGroup(indicatorGroupId); // we set this group view
														// to be hided
				mAdapter.notifyDataSetChanged();
			}
		}
		if (indicatorGroupId == -1) {
			return;
		}

		/**
		 * calculate point (0,indicatorGroupHeight)
		 */
		int showHeight = indicatorGroupHeight;
		int nEndPos = listView.pointToPosition(0, indicatorGroupHeight);
		if (nEndPos != AdapterView.INVALID_POSITION) {
			long pos = listView.getExpandableListPosition(nEndPos);
			int groupPos = ExpandableListView.getPackedPositionGroup(pos);
			if (groupPos != indicatorGroupId) {
				View viewNext = listView.getChildAt(nEndPos
						- listView.getFirstVisiblePosition());
				showHeight = viewNext.getTop();
				Log.e(TAG, PRE
						+ "update the show part height of indicator group:"
						+ showHeight);
			}
		}

		// update group position
		MarginLayoutParams layoutParams = (MarginLayoutParams) indicatorGroup
				.getLayoutParams();
		layoutParams.topMargin = -(indicatorGroupHeight - showHeight);
		indicatorGroup.setLayoutParams(layoutParams);

	}

	public void onScrollStateChanged(AbsListView view, int scrollState) {

	}
}